package com.itheima.reggie.common;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 短信工具类
 */
public class SMSUtils {

    /**
     * 发送短信
     *
     * @param phone 手机号
     * @param code  验证码
     */
    public static void send(String phone, String code) {

        DefaultProfile profile = DefaultProfile
                .getProfile("cn-hangzhou", "LTAI5tAm71sik1zcXxbEFeAC", "qooHveH8QSUKsDX7eX9IdDcArpm6ua");

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试"); //短信签名
        request.setTemplateCode("SMS_154950909"); //短信模板CODE
        request.setPhoneNumbers(phone); //手机号
        request.setTemplateParam("{\"code\":\"" + code + "\"}"); //参数

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}